package com.eascs.web.o2o.scheduler.dao;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.runtime.RuntimeSetting;
import com.eascs.web.o2o.report.constants.FefundConstants;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.List;

@Repository
public class BaseDao {
	@Inject
	JdbcSession jdbcSession;
	@Inject
	JdbcSession jdbcSessionForOracle;

	@Inject
	JdbcSession jdbcSessionForService;
	@Inject
	JdbcSession jdbcSessionForSlaveService;
	/**
	 * 统计表中数量
	 * 
	 * @param sql
	 * @return
	 */
	public Integer count(String sql,Object... params) {
	
		return this.jdbcSessionForOracle.findInteger(sql,params);

	}

	/**
	 * 分页查询
	 * 
	 * @param sqlPageQuery
	 * @return
	 */
	public <T> PageResult<T> findDataWithPage(SqlPageQuery<T> sqlPageQuery) {
		return this.jdbcSessionForOracle.findDataWithPage(sqlPageQuery);
	}
	
	public <T> PageResult<T> findDataWithPageByMySql(SqlPageQuery<T> sqlPageQuery) {
		return this.jdbcSession.findDataWithPage(sqlPageQuery);
	}
	
	public <T> PageResult<T> findDataWithPageByService(SqlPageQuery<T> sqlPageQuery) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findDataWithPage(sqlPageQuery);//只有生产环境有主从隔离（从库只读）
		}
		return this.jdbcSessionForService.findDataWithPage(sqlPageQuery);
	}
	
	public List<Object[]> findParamsForService(String sql,Object...params ){
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findObjectsList(sql, params);//只有生产环境有主从隔离（从库只读）
		}
		return this.jdbcSessionForService.findObjectsList(sql, params);
	}
	
	public List<Object[]> findParamsForOracle(String sql,Object...params ){
		return this.jdbcSessionForOracle.findObjectsList(sql, params);
	}
	
	public List<Object[]> findParams(String sql,Object...params ){
		return this.jdbcSession.findObjectsList(sql, params);
	}

	/**
	 * s 保存该表数据
	 */
	@Transactional(transactionManager = "transactionManager")
	public boolean saveEntity(Object entity) {
		return jdbcSession.insert(entity)>0;
	}
	
	
	@Transactional(transactionManager = "transactionManager")
	public boolean updateEntity(Object entity) {
		return jdbcSession.updateById(entity)>0;
	}

	/**
	 * 删除数据
	 */
	public boolean deleteEntity(String delSql) {
		return jdbcSession.execute(delSql)>0;
	}

	/**
	 * @param sql
	 *            保存
	 */
    @Transactional(transactionManager = "transactionManager")
	public boolean excuteSql(String sql, Object... params) {
		return jdbcSession.execute(sql, params)>0;

	}
    /**
     * 数据中心
     * @param entity
     */
    @Transactional(transactionManager = "transactionManager")
    public void insertDev(Object entity){
		jdbcSession.insert(entity);
	}
    
    /**
	 * 实体集合
	 */
	public List<Object[]> findObjectsList(String sql){
		return jdbcSession.findObjectsList(sql);
	}

	public List<Object[]> findObjectsList(String sql, Object... params){
		return jdbcSession.findObjectsList(sql, params);
	}

	public List<Object[]> findObjectsListByOracle(String sql, Object... params){
		return jdbcSessionForOracle.findObjectsList(sql,params);
	}
    
    public <T> List<T> findSimpleObjectList(String sql, Class<T> requiredType, Object... params){
		return jdbcSession.findSimpleObjectList(sql, requiredType, params);
	}
    
	
    public Integer countMysql(String sql,Object... params) {
		return this.jdbcSession.findInteger(sql, params);

	}
    
    public <T> T findEntity(String sql, Class<T> requiredType, Object... params){
    	return jdbcSessionForOracle.findEntity(sql, requiredType, params);
    }
    
    public <T> List<T> findEntitys(String sql, Class<T> requiredType, Object... params) {

		return jdbcSessionForOracle.findEntitys(sql, requiredType, params);
	}
    
    

    public <T> Object findEntityForMysql(String sql, Class<T> requiredType, Object... params){
    	return jdbcSession.findEntity(sql, requiredType, params);
    }
    
    public<T> List<T>  findEntitysForMysql(String sql, Class<T> requiredType, Object... params){
    	return jdbcSession.findEntitys(sql, requiredType, params);
    }
    

	public String  findString(String sql, Object... params){
		return jdbcSessionForOracle.findString(sql,params);
	}
	
	public String  findStringDev(String sql, Object... params){
		return jdbcSession.findString(sql,params);
	}
}
